﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using QASPRibbon.Commands;
using Schema = QASPRibbon.RibbonSchema;

namespace QASPRibbon.Controls
{
    /// <summary>
    /// Represents a SplitButton control in the SharePoint Server Ribbon.
    /// </summary>
    public class RibbonSplitButton : RibbonButton, IRibbonMenu
    {
        #region JavaScript

        /// <summary>
        /// Client-side script that executes when menu is closed.
        /// </summary>
        public string OnClientMenuClose { get; set; }

        /// <summary>
        /// Client-side script that executes when menu is populated.
        /// </summary>
        public string OnClientMenuOpen { get; set; }

        #endregion

        #region Properties

        ///<summary>
        /// Sections used in root level of this split button.
        ///</summary>
        [Obsolete("Use property Controls instead Sections")]
        public IEnumerable<RibbonMenuSection> Sections { get; set; }

        ///<summary>
        /// Child controls.
        ///</summary>
        public IEnumerable<RibbonMenuSection> Controls { get; set; }

        /// <summary>
        /// A value that indicates whether a menu will be created dynamically (True) or not (False).
        /// The default is False.
        /// </summary>
        public bool PopulateDynamically { get; set; }

        /// <summary>
        /// A value that indicates whether a dynamic menu will be populated only the first time that it is accessed. 
        /// The default is False.
        /// </summary>
        public bool PopulateOnlyOnce { get; set; }

        /// <summary>
        /// A value that indicates whether constructed versions of a dynamic menu is stored to use in next time. 
        /// The default is False.
        /// </summary>
        public bool CacheMenuVersions
        {
            get { return _cacheMenuVersions; }
            set { _cacheMenuVersions = value; }
        }

        #endregion

        #region Internal

        internal override string XmlTag
        {
            get { return Schema.SplitButton; }
        }

        internal IEnumerable<RibbonControl> AllControls()
        {
            var res = new List<RibbonControl>();
            foreach (var section in Controls.Where(s => s.Visible))
            {
                section.NameSpace = FullId + ".Menu";
                res.AddRange(section.AllControls);
                res.Add(section);
            }

            return res;
        }

        internal override IEnumerable<RibbonCommand> ClientCommands
        {
            get
            {
                var res = new List<RibbonCommand>
                              {
                                  new RibbonCommand(FullId, "CheckState",
                                                    string.Format("{0}; {1}; return {2}",
                                                                  OnClientActivate ?? string.Empty, OnClientStateCheck,
                                                                  Enabled ? "true" : "false")),
                                  new RibbonCommand(FullId, Schema.Command, string.Format("{0};", OnClientClick ?? string.Empty)),
                                  new RibbonCommand(FullId, Schema.QueryCommand, string.Format("{0}; {1}; return {2};", OnClientActivate ?? string.Empty, OnClientStateCheck, Enabled ? "true" : "false"))
                              };
                return res;
            }
        }

        internal override XElement AsXElement()
        {
            var element = base.AsXElement();
            element.Add(new XAttribute(Schema.CommandQuery, FullId + Schema.CommandQuery));
            var menuElement = new XElement(Schema.Menu, new XAttribute(Schema.Id, FullId + ".Menu"));
            var sectionIndex = 0;
            foreach (var section in Controls)
            {
                sectionIndex++;
                section.Sequence = sectionIndex;
                section.NameSpace = FullId + ".Menu";
                menuElement.Add(section.AsXElement());
            }
            element.Add(menuElement);
            return element;
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates and initializes a new instance of the RibbonSplitButton.
        /// </summary>
        public RibbonSplitButton() { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonSplitButton.
        /// </summary>
        /// <param name="id">Identifier</param>
        public RibbonSplitButton(string id) : base(id) { }

        /// <summary>
        /// Creates and initializes a new instance of the RibbonSplitButton.
        /// </summary>
        /// <param name="id">Identifier</param>
        /// <param name="title">Title</param>
        public RibbonSplitButton(string id, string title) : base(id, title) { }

        #endregion

        #region Private

        private bool _cacheMenuVersions = true;

        #endregion
    }
}
